﻿using Kevin.Shop.Business.SalesManagement.Components.Order;
using Kevin.Shop.Business.Service.Entity.Helper;
using Kevin.Shop.Business.Service.Entity.SalesManagement.Order;
using RiderShop.Mobile.Common.Component;
using RiderShop.Mobile.Modle.MyAccount;
using RiderShop.Mobile.Modle.Purchase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace RiderShop.Mobile.Controllers.Purchase
{
    public partial class PurchaseController : ThemeController
    {
        /// <summary>
        /// 给好友发送送礼短信
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult SendGreetingOrderSMS(OrderGreetingCardInfo orderGreetingCardInfo)
        {
            if (string.IsNullOrEmpty(orderGreetingCardInfo.CustomerUserName) || string.IsNullOrEmpty(orderGreetingCardInfo.RecipientName) 
                || string.IsNullOrEmpty(orderGreetingCardInfo.RecipientCellPhone)
                 || string.IsNullOrEmpty(orderGreetingCardInfo.OrderNumber)
                )
            {
                return Json(new { Status = false, ErrorMessage = "dataWrong" }, JsonRequestBehavior.AllowGet);
            }

            //check是否已经发送过短信
            var orderDetail = OrderModel.GetOrderDetailInfo(orderGreetingCardInfo.OrderNumber);
            if (orderDetail != null && !string.IsNullOrEmpty(orderDetail.ReceiveCellPhone) && orderDetail.ReceiveAddress == "短信已发请好友收礼")
            {
                return Json(new { Status = false, ErrorMessage = "已发送过短信不能再发送" }, JsonRequestBehavior.AllowGet);
            }

            GreetingOrderModel greetingOrderModel = new GreetingOrderModel();
            orderGreetingCardInfo.CustomerUserId = WebClientContext.Current.UserId;
            //1.发送短信
            var result = greetingOrderModel.SendGreetingOrderSMS(orderGreetingCardInfo);
            if (!result.Status)
            {
                return Json(new { Status = false, ErrorMessage = result.ErrorMessage }, JsonRequestBehavior.AllowGet);
            }

            //2.短信发送成功后更新收货信息
            OrderBasicInfo orderInfo = new OrderBasicInfo();
            orderInfo.OrderNumber = orderGreetingCardInfo.OrderNumber;
            orderInfo.CustomerUserName = orderDetail.CustomerUserName;
            orderInfo.ReceiveCellPhone = orderGreetingCardInfo.RecipientCellPhone;
            orderInfo.ReceiveAddress = "短信已发请好友收礼";
            orderInfo.RegionId = orderDetail.RegionId;
            orderInfo.CSMemo = orderGreetingCardInfo.CustomerUserName + "送礼";

            var response = greetingOrderModel.AddGreetingAddress(orderInfo);
                //更新成功
            if (response.Status)
            {
                return Json(new { Status = true, ErrorMessage = "sendSuccessfully" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { Status = false, ErrorMessage = response.ErrorMessage }, JsonRequestBehavior.AllowGet);
            }
        }

        /// <summary>
        /// 挑选贺卡
        /// </summary>
        /// <param name="cardId">卡号</param>
        /// <param name="orderNumber">订单号</param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public ActionResult ChooseGreetingCard(string orderNumber, string cardId)
        {
            GreetingOrderModel greetingOrderModel = new GreetingOrderModel();
            greetingOrderModel.LoadGreeting(orderNumber);

            return View("~/Themes/rongji/Views/Purchase/GreetingCard/GreetingCard.cshtml", greetingOrderModel);
        }

        /// <summary>
        /// 确认贺卡
        /// </summary>
        /// <param name="orderGreetingCardInfo">贺卡信息</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public JsonResult ConfirmGreetingCard(OrderGreetingCardInfo orderGreetingCardInfo)
        {
            GreetingOrderModel greetingOrderModel = new GreetingOrderModel();
            orderGreetingCardInfo.CustomerUserId = WebClientContext.Current.UserId;
            orderGreetingCardInfo.Title = !string.IsNullOrEmpty(orderGreetingCardInfo.Title) ? orderGreetingCardInfo.Title : string.Empty;
            var response = greetingOrderModel.ConfirmGreetingCard(orderGreetingCardInfo);

            return Json(response);
        }

        /// <summary>
        /// 预览贺卡
        /// </summary>
        /// <param name="cardId">卡号</param>
        /// <param name="orderNumber">订单号</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult PreviewCardGreetingCard(string orderNumber, string cardId)
        {
            if (string.IsNullOrEmpty(orderNumber) || string.IsNullOrEmpty(cardId))
            {
                return new EmptyResult();
            }
            GreetingOrderModel model = new GreetingOrderModel();
            model.LoadOrderInfo(orderNumber);
            model.LoadGreeting(orderNumber);
            if (model.OrderInfo == null || model.OrderGreetingCardInfo == null)
            {
                return new EmptyResult();
            }

            return View("~/Themes/rongji/Views/Purchase/GreetingCard/PreviewCard.cshtml", model);
        }

        /// <summary>
        /// 填写收货信息接收礼品订单
        /// </summary>
        /// <param name="orderNumber">订单号</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ReceiveGreetingOrder(string orderNumber)
        {
            if (string.IsNullOrEmpty(orderNumber))
            {
                return new EmptyResult();
            }
            GreetingOrderModel model = new GreetingOrderModel();
            model.LoadOrderInfo(orderNumber);
            //model.LoadGreeting(orderNumber);
            if (model.OrderInfo == null)
            {
                return new EmptyResult();
            }

            return View("~/Themes/rongji/Views/Purchase/GreetingCard/ReceiveGreetingOrder.cshtml", model);
        }

        /// <summary>
        /// 添加送礼订单的收货地址
        /// </summary>
        /// <param name="orderInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddGreetingAddress(OrderBasicInfo orderInfo)
        {
            GreetingOrderModel model = new GreetingOrderModel();
            var response = model.AddGreetingAddress(orderInfo);
            if (response.Status)
            {
                return Json("");
            }
            else
            {
                return Json(response.ErrorMessage);
            }
        }
    }
}
